<template>
    <div class="mainDiv" style="padding: 10px;box-sizing: border-box;">
<el-form ref="form" class="mt">
                    <el-form-item label="职位或职称" label-width="84px">
                        <el-input placeholder="请输入职位或职称名称" v-model="name"></el-input>
                        <!-- 阻止默认的点击enter键提交事件 -->
                        <el-input style="display: none;"></el-input>
                    </el-form-item>
                    <el-form-item label="是否显示门店权限">
                        <el-switch v-model="showmd" on-text="是" off-text="否" v-if="selectshowmd === 1"></el-switch>
                        <el-switch v-model="showmd" on-text="是" off-text="否" v-else disabled></el-switch>
                    </el-form-item>
                </el-form>

            <el-row>
            <el-col :span="12">
                <el-button @click.native="close" style="width:90%;display:block;">取消</el-button>
            </el-col>
            <el-col :span="12">
                <el-button type="success" @click.native="callSubmit" style="width:90%;display:block;" :loading="loading">确定</el-button>
            </el-col>
        </el-row>    
    </div>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'positionAdd',
  data () {
    return {
      loading: false,
      id: 0,
      name: '',
      positionid: 0,
      showmd: false,
      selectshowmd: 1
    }
  },
  async mounted () {
    var that = this
    let row = C.getDatas()
    if (row.po) {
      this.positionid = row.po
    }
    if (row.obj) {
      let obj = row.obj
      this.id = obj.id
      this.name = obj.name
      this.positionid = obj.positionid
      this.showmd = !!obj.showmd
      if (obj.positionid !== 0) {
        let data = await C.ajax('/organization/zhiwei/addposition', { id: obj.positionid }, 'post')
        if (data) {
          data.forEach((row) => {
            that.selectshowmd = row.showmd
          })
        }
      }
    }
  },
  methods: {
    async callSubmit (index) {
      let that = this
      if (/^\s*$/.test(this.name)) {
        C.error('名称不能有空格！')
        return false
      }
      let data = {
        id: this.id,
        name: this.name,
        positionid: this.positionid,
        showmd: this.showmd ? 1 : 0
      }
      this.loading = true
      let res = await C.ajax('/organization/zhiwei/add', data, 'post')
      if (res) {
        if (res.errno === 0) {
          that.loading = false
          that.$notify({
            title: '成功',
            message: res.data,
            type: 'success'
          })
          C.closeBrowserWindow(res)
        } else {
          that.loading = false
          that.$notify.error({
            title: '错误',
            message: res.errmsg
          })
        }
      }
    },
    close () {
      C.closeBrowserWindow()
    }
  }
}
</script>